﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-17 11:52:29
 * 功能：界面【GuidePanel】的View
        1、负责收集所有控件
**************************************************************************************/

using Game.Framework;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.GuidePanel
{
    public class GuidePanelView : BasePanel
    {
        public const string PANEL_KEY = "Game_UI_GuidePanel_GuidePanel_Key";
        private HollowOut m_Hollow;
        private GuideData m_Data;

        protected override void RegistAny()
        {
            PanelManager.Instance.RegistPanel(PANEL_KEY, this);
            EventCenter.Instance.AddEventListen<GuideData>(GameEventName.GUIDE, OnGuide);
            EventCenter.Instance.AddEventListen(GameEventName.HIDE_GUIDE, OnEnable);
        }

        protected override void UnregistAny()
        {
            PanelManager.Instance.UnregistPanel(PANEL_KEY);
            EventCenter.Instance.RemoveEventListen<GuideData>(GameEventName.GUIDE, OnGuide);
            EventCenter.Instance.RemoveEventListen(GameEventName.HIDE_GUIDE, OnEnable);
        }

        protected override void InitUI()
        {
            m_Hollow = GetControl<Button>(GuidePanelNotifications.Mask_CTRL).GetComponent<HollowOut>();
        }

        private void OnEnable()
        {
            m_Data = null;
            m_Hollow.gameObject.SetActive(false);
            GetControl<Image>(GuidePanelNotifications.Tip_CTRL).gameObject.SetActive(false);
        }

        private void OnGuide(GuideData data)
        {
            m_Data = data;
            if (data == null) throw new Exception("引导数据不能为空！！");
            m_Hollow.gameObject.SetActive(true);
            GetControl<Image>(GuidePanelNotifications.Tip_CTRL).rectTransform.sizeDelta = new Vector2(data.isSmall ? 450 : 735, 0);
            GetControl<Image>(GuidePanelNotifications.Tip_CTRL).gameObject.SetActive(false);
            var rot = Quaternion.Euler(new Vector3(m_Data.flipH ? 180 : 0, 0, m_Data.flipV ? 180 : 0));
            GetControl<Image>(GuidePanelNotifications.Tip_CTRL).transform.localRotation = rot;
            GetControl<Text>(GuidePanelNotifications.Content_CTRL).text = m_Data.tip;
            GetControl<Text>(GuidePanelNotifications.Content_CTRL).transform.localRotation = rot;
            GetControl<Image>(GuidePanelNotifications.Tip_CTRL).rectTransform.anchoredPosition = data.tipPos;

            // 更新点击区域和高亮区域
            if (m_Data.isResetHigh)
                m_Hollow.UpdateHighArea(null);
            m_Hollow.UpdateHighArea(m_Data.highArea, () =>
            {
                GetControl<Image>(GuidePanelNotifications.Tip_CTRL).gameObject.SetActive(true);
            });
        }

        protected override void ClickBtnHandle(string name)
        {
            if (m_Data == null) return;
            m_Data.onClickContinueBtnHandle?.Invoke();
        }
    }
}